package com.zhongketech.sunflower.control;

import java.util.ArrayList;
import java.util.List;

import com.zhongketech.sunflower.MyApplication;
import com.zhongketech.sunflower.transport.LogUtil;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.widget.Toast;

public class AppReceiver extends BroadcastReceiver{

	private static final String LOGCAT = "AppReceiver";
	private static  Context context = null;

	@Override
	public void onReceive(Context context, Intent intent){

		this.context = context;

		LogUtil.i(LOGCAT, "intent.getAction: "+intent.getAction());
		//接收安装广播 
		if (intent.getAction().equals("android.intent.action.PACKAGE_ADDED")) {   
			//TODO
			String packageName = getPackageName(intent.getDataString());   
			LogUtil.i(LOGCAT, "packageName:"+packageName);

		}   
		//接收卸载广播  
		if (intent.getAction().equals("android.intent.action.PACKAGE_REMOVED")) {   
			//TODO
			String packageName = intent.getDataString();   
			LogUtil.i(LOGCAT, "PACKAGE_REMOVED packageName:"+packageName);
			//Cmd.exeCmd("pm uninstall -k "+packageName);
		}
	}

	private static String  getAppName(String packageName)
	{
		String appname = null;
		try {
			appname = context.getPackageManager().getPackageInfo(packageName, 1)
					.applicationInfo.loadLabel(MyApplication.getInstance().getApplicationContext().getPackageManager()).toString();
		} catch (NameNotFoundException e1) {
			e1.printStackTrace();
		}

		return appname;
	}


	private static String getPackageName(String intentPackageName)
	{
		String packageName = intentPackageName;
		if(null != intentPackageName)
		{
			String list[] = intentPackageName.split(":");
			if(list.length >= 2)
			{
				packageName = list[list.length - 1];
			}
		}

		return packageName;
	}
}
